// pages/FirstEdit/FirstEdit.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    classify_name: '',
    isOpen: '',
    judgeState: true,
    classify_id: '',
    store_id: '',
    level: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.classify_id) {
      this.setData({
        classify_id: options.classify_id
      });
    }
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    if (options.level) {
      this.setData({
        level: options.level
      });
    }
    this.getJson();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getJson() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.detail-category');
    let json = {
      id: that.data.classify_id
    };
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            classify_name: res.data.name
          });
          if (res.data.enabled == 1) {
            that.setData({
              isOpen: '1'
            });
          } else if (res.data.enabled == 0) {
            that.setData({
              isOpen: '0'
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  classifyNameinp(e) {
    let val = e.detail.value;
    this.setData({
      classify_name: val
    });
  },
  isOpenChange(e) {
    let val = e.detail;
    this.setData({
      isOpen: val
    });
  },
  //确定编辑
  sureEdit() {
    let isEnabled = '';
    if (this.data.isOpen == '1') {
      isEnabled = 1;
    } else if (this.data.isOpen == '0') {
      isEnabled = 0;
    }
    var that = this;
    that.judge();
    if (that.data.judgeState == true) {
      let json = {
        store_id: that.data.store_id,
        name: that.data.classify_name,
        enabled: isEnabled,
        id: that.data.classify_id,
        level: that.data.level,
      };
      let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.edit-category');
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  //要延时执行的代码
                  wx.navigateTo({
                    url: '/packageC/o2o/GoodsClassify/GoodsClassify?store_id=' + that.data.store_id
                  });
                }, 1500); //延迟时间
              }
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        },
        fail: function(res) {}
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '分类信息不完整',
        duration: 1500
      });
    }
  },
  judge() {
    this.setData({
      judgeState: true
    });
    if (this.data.classify_name == '' || this.data.isOpen == '') {
      this.setData({
        judgeState: false
      });
    }
  },
});
